package com.ym.core.utils.excel;

import cn.hutool.core.exceptions.ExceptionUtil;

import com.ym.core.exception.BusinessException;
import com.ym.core.utils.ReflectUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author: zhangfengzhou
 * Date: 2019-04-16
 * Time: 11:53
 */
public class ImportExcelUtil {
	private static Logger logger = LoggerFactory.getLogger(ImportExcelUtil.class);

	//医院模板
	public static Map<String, ExcelBean> getHospitalTemplate() {
		Map<String, ExcelBean> subHospitalFieldCols = new LinkedHashMap<>();
		subHospitalFieldCols.put("hosName", new ExcelBean(0, String.class));
		subHospitalFieldCols.put("hosCode", new ExcelBean(1, String.class));
		subHospitalFieldCols.put("hosLevel", new ExcelBean(2, String.class));
		subHospitalFieldCols.put("prov", new ExcelBean(3, String.class));
		subHospitalFieldCols.put("city", new ExcelBean(4, String.class));
		subHospitalFieldCols.put("area", new ExcelBean(5, String.class));
		subHospitalFieldCols.put("town", new ExcelBean(6, String.class));
		subHospitalFieldCols.put("addrDesc", new ExcelBean(7, String.class));
		subHospitalFieldCols.put("phoneNo", new ExcelBean(8, String.class));
		subHospitalFieldCols.put("email", new ExcelBean(9, String.class));
		subHospitalFieldCols.put("introduction", new ExcelBean(10, String.class));
		//subHospitalFieldCols.put("town",new ExcelBean(21,String.class));
		return subHospitalFieldCols;
	}

	//科室模板
	public static Map<String, ExcelBean> getHosDeptTemplate() {
		Map<String, ExcelBean> subHosDeptFieldCols = new LinkedHashMap<>();
		subHosDeptFieldCols.put("hosCodeUni", new ExcelBean(0, String.class));
		subHosDeptFieldCols.put("deptCode", new ExcelBean(1, String.class));
		subHosDeptFieldCols.put("deptName", new ExcelBean(2, String.class));
		subHosDeptFieldCols.put("deptType", new ExcelBean(3, String.class));
		subHosDeptFieldCols.put("parentDeptCode", new ExcelBean(4, String.class));
		//subHosDeptFieldCols.put("standardDeptId",new ExcelBean(5,Long.class));
		return subHosDeptFieldCols;
	}

	//医生模板
	public static Map<String, ExcelBean> getDoctorTemplate() {
		Map<String, ExcelBean> subDoctorFieldCols = new LinkedHashMap<>();
		subDoctorFieldCols.put("hospitalCode", new ExcelBean(0, String.class));
		subDoctorFieldCols.put("deptCode", new ExcelBean(1, String.class));
		subDoctorFieldCols.put("doctCategory", new ExcelBean(2, String.class));
		subDoctorFieldCols.put("doctCode", new ExcelBean(3, String.class));
		subDoctorFieldCols.put("realName", new ExcelBean(4, String.class));
		subDoctorFieldCols.put("sex", new ExcelBean(5, String.class));
		subDoctorFieldCols.put("phoneNo", new ExcelBean(6, String.class));
		subDoctorFieldCols.put("cardType", new ExcelBean(7, String.class));
		subDoctorFieldCols.put("cardNo", new ExcelBean(8, String.class));
		subDoctorFieldCols.put("doctType", new ExcelBean(9, String.class));
		subDoctorFieldCols.put("jobTitle", new ExcelBean(10, String.class));
		return subDoctorFieldCols;
	}

	//HIS科室模板
	public static Map<String, ExcelBean> getHISDeptTemplate() {
		Map<String, ExcelBean> subHisDeptFieldCols = new LinkedHashMap<>();
		//subHisDeptFieldCols.put("hosName", new ExcelBean(0, String.class));
		//subHisDeptFieldCols.put("hosCode", new ExcelBean(1, String.class));
		subHisDeptFieldCols.put("deptName", new ExcelBean(0, String.class));
		subHisDeptFieldCols.put("deptCode", new ExcelBean(1, String.class));
		subHisDeptFieldCols.put("deptDesc", new ExcelBean(2, String.class));
		subHisDeptFieldCols.put("clinicAttr", new ExcelBean(3, String.class));
		subHisDeptFieldCols.put("outpOrInp", new ExcelBean(4, String.class));
		subHisDeptFieldCols.put("deptType", new ExcelBean(5, String.class));
		subHisDeptFieldCols.put("roomName", new ExcelBean(6, String.class));
		subHisDeptFieldCols.put("roomCode", new ExcelBean(7, String.class));
		return subHisDeptFieldCols;
	}

	//HIS医生模板
	public static Map<String, ExcelBean> getHISDoctorTemplate() {
		Map<String, ExcelBean> subHisDoctorFieldCols = new LinkedHashMap<>();
		subHisDoctorFieldCols.put("doctorName", new ExcelBean(0, String.class));
		subHisDoctorFieldCols.put("doctorStaffNo", new ExcelBean(1, String.class));
		subHisDoctorFieldCols.put("deptName", new ExcelBean(2, String.class));
		subHisDoctorFieldCols.put("deptCode", new ExcelBean(3, String.class));
		subHisDoctorFieldCols.put("title", new ExcelBean(4, String.class));
		subHisDoctorFieldCols.put("job", new ExcelBean(5, String.class));
		subHisDoctorFieldCols.put("edu", new ExcelBean(6, String.class));
		subHisDoctorFieldCols.put("info", new ExcelBean(7, String.class));
		subHisDoctorFieldCols.put("expert", new ExcelBean(8, String.class));
		return subHisDoctorFieldCols;
	}

	/**
	 * @return
	 * @throws IOException
	 */
	public static <T> List<T> readExcel
	(InputStream inputStream, String fileName, int startLine, Class<T> clz, Map<String, ExcelBean> fieldCols) throws Exception {
		T obj = null;
		List<T> list = new ArrayList<T>();
		Workbook work = ExcelUtil.getWorkbook(inputStream, fileName);
		/**开始*/
		Sheet sheet = work.getSheetAt(0);//工作簿
		Row row = null;//行
		Cell cell = null;//列
		//遍历Excel中的所有sheet
		if (null == sheet) {
			return list;
		}
		//判断是否是导入模板
		row = sheet.getRow(0);
		cell = row.getCell(0);
		String title = String.valueOf(cell);
		if (!title.contains("导入模板")){
			throw new BusinessException("请下载正确格式的导入模板");
		}
		int firstRowNum = sheet.getFirstRowNum() + startLine;
		int lastRowNum = sheet.getLastRowNum();
		try {
			for (int rindex = firstRowNum; rindex <= lastRowNum; rindex++) {
				//System.out.println("rindex======" + rindex);
				row = sheet.getRow(rindex);
				if (row != null) {
					String fieldName;
					ExcelBean excelToBean;
					Row curretRow = sheet.getRow(rindex);
					Object newCell = 0;
					//创建对象
					obj = clz.newInstance();

					for (Map.Entry<String, ExcelBean> fieldCol : fieldCols.entrySet()) {
						fieldName = fieldCol.getKey();//字段名
						excelToBean = fieldCol.getValue();//列数和字段类型
						Method setMethod = clz.getMethod(ReflectUtils.getSetMethodNameFromFiledName(fieldName), excelToBean.getParameterType());
						if (excelToBean.getColNum() < curretRow.getLastCellNum()) {
							// cell = row.getCell(excelToBean.getColNum());
							Object objCell = ExcelUtil.getCellValue(row.getCell(excelToBean.getColNum()));
							String str = (String) objCell;
							if (null == str) {
								continue;
							}
							newCell = str;
							/*String reg = "[0-9]*";
							Pattern pattern = Pattern.compile(reg);
							Matcher isNum = pattern.matcher(str);
							if (isNum.matches() && str.length() < 6) {
								newCell = Integer.parseInt(str);
							} else {
								newCell = objCell;
							}*/
							setMethod.invoke(obj, newCell);
						}
					}
				}
				list.add(obj);
			}
		} catch (Exception e) {
			logger.error(ExceptionUtil.getMessage(e));
			logger.error(ExceptionUtil.getMessage(e.getCause()));
			logger.error(e.getMessage());
			logger.error("文件格式或文件数据有误:" + e.getMessage());
			throw new BusinessException("文件格式或文件数据有误！");
		} finally {
			inputStream.close();
		}
		return list;
	}
}
